<template>
  <div class="goods-info">
    <!-- 购物车小球 -->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballflag" ref="ball"></div>
    </transition>

    <!-- 图片轮播区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :imageList="goodsImages" :isfull="false"></swiper>
        </div>
      </div>
    </div>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsInfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{ goodsInfo.market_price }}</del> &nbsp;&nbsp;
            销售价：
            <span class="price-now">￥{{ goodsInfo.sell_price }}</span>
          </p>
          <div class="buy">
            购买数量：
            <numbox @getcount="getSelectedCount" :nummax="goodsInfo.stock_quantity"></numbox>
          </div>
          <p>
            <mt-button type="danger">立即购买</mt-button>
            <mt-button type="primary" @click="addShopCar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <!-- 商品详情 和评论 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{ goodsInfo.goods_no }}</p>
          <p>库存情况：{{ goodsInfo.stock_quantity }}件</p>
          <p>上架时间：{{ goodsInfo.add_time | dateFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import swiper from "../common/swiper.vue";
import numbox from "../common/goodsInfo_numbox.vue";
import { Toast } from "mint-ui";
import { parse } from "path";
export default {
  data() {
    return {
      // 商品 id
      id: this.$route.params.id,
      goodsImages: [], // 商品轮播图片
      goodsInfo: {}, // 商品信息（价格等）
      ballflag: false, // 小球是否显示
      selectedCount: 1 // 选择了几个商品
    };
  },
  methods: {
    //   获取商品详情的轮播图
    getGoodsImages() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsImages = result.body.message;
        }
      });
    },
    //  获取商品 信息
    getGoodsInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsInfo = result.body.message[0];
        } else {
          Toast("加载失败！");
        }
      });
    },
    // 点击跳转到 图文介绍， 用编程式导航
    goDesc(id) {
      this.$router.push({ name: "goodsDesc", params: { id } });
    },
    // 点击跳转 商品评论
    goComment(id) {
      this.$router.push({ name: "goodsComment", params: { id } });
    },
    // 添加到购物车
    addShopCar() {
      this.ballflag = !this.ballflag;
      //  这是要添加到购物车的商品数据，然后保存到 store 中
      var goodsMsg = {
        id: this.id,
        count: parseInt(this.selectedCount),
        price: this.goodsInfo.sell_price,
        selected: true
      };
      this.$store.commit("addToCar", goodsMsg);
    },
    //  ---------------- 动画开始 --------------
    beforeEnter(el) {
      // el.style.opacity = 0;
      el.style.transform = "translate(0, 0)";
      // el.style.width = el.style.height = "40px";
    },
    enter(el, done) {
      // el.offsetHeight;
      el.style.opacity = 1;
      // el.style.width = el.style.height = "15px";

      // 获取小球在页面中的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();

      // 获取购物车 徽标的位置
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();

      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px, ${yDist}px)`;

      // el.style.transform = `translate(200px, 200px)`;
      el.style.transition = "all 0.5s cubic-bezier(.4, -0.3, 1, .68)";
      done();
    },
    afterEnter(el) {
      this.ballflag = !this.ballflag;
    },
    // ----------------- 动画结束 ----------------

    getSelectedCount(count) {
      // 把子组件传过来的值，保存在 data 上
      this.selectedCount = count;
    }
  },
  components: {
    swiper,
    numbox
  },
  created() {
    this.getGoodsImages();
    this.getGoodsInfo();
  }
};
</script>

<style lang="scss" scoped>
.goods-info {
  position: relative;
  background: #eee;
  overflow: hidden;
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    z-index: 99;
    top: 345px;
    left: 145px;
    text-align: center;
    color: #fff;
  }
  .price {
    .price-now {
      color: #ff4400;
      font-size: 18px;
      font-weight: 700;
    }
  }
  .buy {
    margin-bottom: 5px;
  }
  .mui-card-header {
    font-size: 18px;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }
}
</style>